package com.aaa.member.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.entity.TbOrder;
import com.aaa.member.service.TbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (TbOrder)表控制层
 *
 * @author makejava
 * @since 2020-11-18 09:06:05
 */
@RestController
@RequestMapping("tbOrder")
public class TbOrderController {
    /**
     * 服务对象
     */
    @Resource
    private TbOrderService tbOrderService;


    /**
     * 后台分页+查询所有订单
     * @param pageNo
     * @param pageSize
     * @param memberName
     * @param orderstate
     * @param brandtypename
     * @return
     */
    @GetMapping("queryAllOrder")
    public Result queryAllOrder(Integer pageNo, Integer pageSize, String memberName, Integer orderstate, String brandtypename,String vin){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return tbOrderService.queryAllOrder(pageNo,pageSize,memberName,orderstate,brandtypename,vin);
    }
    /**
     * 客户租车下订单
     * @param tbOrder
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody TbOrder tbOrder){
        //计算消费金额，更新订单总金额
        //向账号流水表添加数据
        //计算积分，添加客户积分历史，更新成长值，账户余额，更新用户积分和历史积分
        //获取该用户的优惠券，添加客户优惠券关联表
        //更新车辆状态
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.tbOrderService.insert(tbOrder));
    }
    /**
     * 修改状态为未完成的订单信息
     * @param order
     * @return
     */
    @PutMapping("updateToOrder")
    public Result update(@RequestBody  TbOrder order){
        //当ID空为时抛出自定义异常
        if (order.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbOrderService.update(order));
    }

    /**
     * 删除订单为未完成的订单信息
     * @param orderId
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer orderId){
        //当ID空为时抛出自定义异常
        if (null == orderId) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbOrderService.deleteById(orderId));
    }
}